package com.ruoyi.common;/**
 * Created by wfly on 2018/8/29.
 */

import io.swagger.models.auth.In;
import org.apache.shiro.crypto.hash.Hash;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.concurrent.*;


/**
 * @Description TODO
 */
 class Test {
    public static void  test1(){
        StringBuffer str = new StringBuffer("123");
        StringBuilder sbr = new StringBuilder();
        str.append("1243");
        char[] s1 = "12345".toCharArray();
        char[] s2 = "67890".toCharArray();
        char[] s3 = new char[s1.length+s2.length];
        System.arraycopy(s1,0,s3,0,s1.length);
        System.arraycopy(s2,0,s3,s1.length,s2.length);
        System.out.println(s3);
        System.out.println(Arrays.copyOf(s3,30).length);
        List lit = new ArrayList<>();
        List linkList = new LinkedList();
        Map map1 = new HashMap();
        Map mapt = new Hashtable();
        Map mapc = new ConcurrentHashMap();
    }
    public static void  test2() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        A.a = 6;
        Class clazz = Class.forName("com.ruoyi.common.A");
        System.out.println(A.a);
        A a1 = (A) clazz.newInstance();
        System.out.println(A.a);
    }
    public static void  test3(){
        C proxy = (C) new ProxyFactory(new A()).proxyObjInstance();
        proxy.fun();
        System.out.println(proxy.fun1());
    }
    public static void  test4(){
        int j=2;
        if(j*0.1==0.2){
            System.out.println("j="+j);
        }
        if(j++*0.1==0.3){
            System.out.println("j="+j);
            ++j;
        }
        if(++j*0.1==0.4){
            System.out.println("j="+j++);
        }
        j++;
        System.out.println("j="+j);
    }
    public static void  test5(){
        new Thread(new Runnable() {
           @Override
           public void run() {

           }
       }).start();
    }


    public static void  test6() throws ExecutionException, InterruptedException {
        CallTest callTest1 = new CallTest(0);
        CallTest callTest2 = new CallTest(5);
       //  ExecutorService pool = Executors.newFixedThreadPool(2);
        ExecutorService pool = Executors.newSingleThreadExecutor();
        Future<Integer> future1 = pool.submit(callTest1);
      //  Future<Integer> future2 = pool.submit(callTest2);
        System.out.println("=========其他操作========");
        int re = future1.get();
        //int re = future2.get()+future1.get();
        System.out.println("future count result:"+re);
        System.out.println("======end=====");
       // pool.shutdown(); // 创建的线程要关闭
    }
    public static void main(String[] args) throws Exception {
        test6();
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("======gc====");
        super.finalize();
    }
}
class CallTest implements Callable{
    private int a;
    public CallTest(int a){
        this.a = a;
    }
    @Override
    public Object call() throws Exception {
        System.out.println("执行call:a="+a);
        if(a==5){
            Thread.sleep(1000);
        }
        int re=0;
        for(int i=0;i<5;i++){
            a++;
            re+=a;
        }

        return re;
    }
}
class ProxyFactory{
   private Object target;
    public ProxyFactory(Object object){
        this.target = object;
    }
    public Object proxyObjInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("代理执行前：");
                        Object returnValue = method.invoke(target, args);
                        System.out.println("代理执行后：");
                        return returnValue;
                    }
                });
    }

}
class Singleton{
    private Singleton(){}
    public static Singleton getInstance(){
        return InnerC.singleInstance;
    }
    private static class InnerC{ public static final  Singleton  singleInstance = new Singleton();}
}
class Singleton1{
    private Singleton1(){}
   private static Singleton1 instance = new Singleton1();

    public static Singleton1 getInstance() {
        return instance;
    }

}

class Singleton2 {

    private Singleton2() {
    }

    private static Singleton2 instance;
    public static Singleton2 getInstance() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }
}

